<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>手动实现路由</title>
    <script src="./js/vue.js"></script>
</head>

<body>
    <div id="app">
        <a href="#/zhuye">主页</a>
        <a href="#/caijing">财经</a>
        <a href="#/keji">科技</a>
        <a href="#/gupiao">股票</a>

        <component :is="mainTap"></component>
    </div>

    <script>

        // 局部组件的申明
        var zhuye = {
            template: '<h1>我是主页</h1>'
        };
        var caijing = {
            template: '<h1>我是财经</h1>'
        };
        var keji = {
            template: '<h1>我是科技</h1>'
        };
        var gupiao = {
            template: '<h1>我是股票</h1>'
        }

        var vm = new Vue({
            el: '#app',
            data: {
                mainTap: 'zhuye'
            },
            components: {
                zhuye,
                caijing,
                keji,
                gupiao
            }
        })

        // 监听网页hash值的变化
        window.onhashchange = function () {

            console.log(location.hash.slice(1));

            switch (location.hash.slice(1)) {

                case "/zhuye":
                    vm.mainTap = 'zhuye'
                    break;
                case "/caijing":
                    vm.mainTap = 'caijing'
                    break;
                case "/keji":
                    vm.mainTap = 'keji'
                    break;
                case "/gupiao":
                    vm.mainTap = 'gupiao'
                    break;

            }
        }
    </script>
</body>

</html>